/**
 * 金额格式化（添加千分位）
 * @param  {Number|String} money   金额
 * @param  {Object} options 配置参数
 * @return {String}      格式化后的金额字符串
 */
export const moneyDivide = (money, options = {}) => {
	if(!money || isNaN(money)) {
		money = '0';
	}
	money = money.toString();

	const moneyArr = money.split('.');

	const intArr = moneyArr[0].split('');

	const intDivideArr = intArr.map((item, i) => {
		const index = intArr.length - i;

		if(index === intArr.length || index % 3 !== 0) {
			return item;
		} else {
			return `,${item}`;
		}
	});

	let decimalStr;

	if(!moneyArr[1]) {
		decimalStr = `00`;
	} else if(moneyArr[1].length === 1) {
		decimalStr = `${moneyArr[1]}0`;
	} else {
		decimalStr = moneyArr[1];
	}

	return `${intDivideArr.join('')}.${decimalStr}`;
}

/**
 * 金额格式化（万、亿省略显示）
 * @param  {Number} money 金额
 * @return {String}       格式化后的金额字符串
 */
export const moneyTruncate = money => {
	if(!money || isNaN(money)) {
		money = '0';
	}

	money = Number(money);

	if(money < 10000) {
		return `${toFixed(money)}元`;
	} else if(money >= 10000 && money < 100000000) {
		return `${toFixed(money/10000)}万`;
	} else {
		return `${toFixed(money/100000000)}亿`;
	}
}

/**
 * 强行保留两位小数
 * @param  {Number} number 数字
 * @return {void}
 */
export const toFixed = number => {
	const num = Math.floor(Math.round(number * 100)) / 100;
	return num.toFixed(2);
}
/**
 * 阿拉伯数字转换为中文文字
 * @param section
 * @returns {string}
 * @constructor
 */
export const sectionToChinese=(section)=>{
  const chnNumChar = ["零","一","二","三","四","五","六","七","八","九"];
  const chnUnitChar = ["","十","百","千"];

  let strIns = '', chnStr = '';
  let unitPos = 0;
  let zero = true;
  let skipTen =false;
  if(Math.floor(section / 10)===1){
    skipTen = true;
  }
  while(section > 0){
    let v = section % 10;
    if(v === 0){
      if(!zero){
        zero = true;
        chnStr = chnNumChar[v] + chnStr;
      }
    }else{
      zero = false;
      strIns = chnNumChar[v];
      if(skipTen&&strIns==='一'){
        strIns='';
      }
      strIns += chnUnitChar[unitPos];
      chnStr = strIns + chnStr;
    }
    unitPos++;
    section = Math.floor(section / 10);
  }
  return chnStr;
}